package com.nmgsr.ptmsapp.activity;

import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.nmgsr.beans.requestdao.PageReq;
import com.nmgsr.beans.responsedao.PtmsBorrowRecordRes;
import com.nmgsr.beans.responsedao.PtmsWarnTestRes;
import com.nmgsr.beans.service.Api;
import com.nmgsr.nettools.base.BaseActivity;
import com.nmgsr.nettools.net.config.NetManager;
import com.nmgsr.nettools.net.retrofit.BaseResponse;
import com.nmgsr.nettools.net.retrofit.HttpHelper;
import com.nmgsr.nettools.net.retrofit.HttpResponseObserver;
import com.nmgsr.nettools.net.retrofit.HttpUtil;
import com.nmgsr.nettools.net.retrofit.PagedResult;
import com.nmgsr.ptmsapp.R;
import com.nmgsr.ptmsapp.adapter.ListPtmsBorrowRecordAdapter;
import com.nmgsr.ptmsapp.adapter.ListPtmsWarnTestAdapter;
import com.nmgsr.ptmsapp.config.PreferencesManager;
import com.nmgsr.ptmsapp.view.MyDividerItemDecoration;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

public class ListPtmsBorrowRecordActivity extends BaseActivity {
    private static final String TAG = ListPtmsBorrowRecordActivity.class.getSimpleName();
    private EditText et_search;
    private SmartRefreshLayout refreshLayout;

    private ListPtmsBorrowRecordAdapter adapter;
    private int page = 1;
    private String token;
    private List<String> photoList = new ArrayList<>();
    @Override
    protected int getLayoutId() {
        return R.layout.list_ptms_borrow_record_activity;
    }
    @Override
    protected void initView() {
        super.initView();
        mContext = ListPtmsBorrowRecordActivity.this;
        fl_actionbar.setVisibility(View.GONE);
        findViewById(R.id.iv_back).setOnClickListener(v -> finish());
        token = PreferencesManager.getToken(mContext);
        photoList.add("https://img1.baidu.com/it/u=51815545,3352787065&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500");
        photoList.add("https://img2.baidu.com/it/u=1633175549,4055880068&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=375");
        photoList.add("https://img.dlwjdh.com/upload/13105/210712/f06ae66511c7fd0b76544f732351a730.jpg");

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        MyDividerItemDecoration divider = new MyDividerItemDecoration(mContext, MyDividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.custom_divider));
        recyclerView.addItemDecoration(divider);

        adapter = new ListPtmsBorrowRecordAdapter();
        recyclerView.setAdapter(adapter);

        refreshLayout = findViewById(R.id.refresh_layout);
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            getData();
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            getData();
        });
        //adapter点击事件
        adapter.setOnItemClickListener((adapter, view, position) -> {
            ImageActivity.startActivity(mContext, (ArrayList<String>) photoList, 0,false);

        });

    }

    protected void initData() {
        refreshLayout.autoRefresh();

    }

    protected void onResume() {
        super.onResume();
        initData();
    }

    private void getData() {
        PageReq pageReq = new PageReq();
        pageReq.setPage(page);
        pageReq.setPageSize(20);
        NetManager.getInstance().setAuthorizationToken(token);
        HttpHelper.getInstance()
                .getService(Api.class)
                .getPtmsBorrowRecordList(pageReq)
                .compose(HttpUtil.defaultSchedulers())
                .compose(bindToLifecycle())
                .subscribe(new HttpResponseObserver<BaseResponse<PagedResult<PtmsBorrowRecordRes>>>() {

                    @Override
                    protected void onSuccess(BaseResponse<PagedResult<PtmsBorrowRecordRes>> response) {
                        Log.e(TAG, "PtmsToolResponse: " + new Gson().toJson(response));
                        if (response.isSuccess()) {
                            List<PtmsBorrowRecordRes> roleList = response.getResult().getItems();
                            if (page == 1) {
                                if (roleList != null && !roleList.isEmpty()) {
                                    // 更新UI数据
                                    adapter.setList(roleList);
                                    refreshLayout.finishRefresh();
                                } else {
                                    QMUIEmptyView qmuiEmptyView = new QMUIEmptyView(mContext);
                                    qmuiEmptyView.show(false, "暂无数据", null, getString(R.string.emptyView_mode_desc_retry),
                                            v -> refreshLayout.autoRefresh());
                                    adapter.setEmptyView(qmuiEmptyView);
                                    refreshLayout.finishRefresh();
                                }
                            } else {
                                adapter.addData(roleList);
                                refreshLayout.finishLoadMore();
                            }
                        } else {
                            Log.e(TAG, "业务错误：" + response.getMessage());
                            Toast.makeText(mContext, "错误：" + response.getMessage(), Toast.LENGTH_SHORT).show();
                            refreshLayout.finishRefresh(false);
                        }
                    }

                    @Override
                    protected void handleError(Throwable e) {
                        Log.e(TAG, "网络异常：", e);
                    }
                });
    }
}
